package com.blue.wechat.receive.parse;

import com.blue.wechat.receive.model.AbstractResponse;
import com.blue.wechat.receive.model.ArticleItem;
import com.blue.wechat.receive.model.ImageResponse;
import com.blue.wechat.receive.model.MusicResponse;
import com.blue.wechat.receive.model.NewsResponse;
import com.blue.wechat.receive.model.TextResponse;
import com.blue.wechat.receive.model.VideoResponse;
import com.blue.wechat.receive.model.VoiceResponse;

public class XmlModelBuilder
{
	private XmlModelBuilder()
	{
	}

	public static String build(AbstractResponse response)
	{
		if (response == null)
			return "";
		
		StringBuilder xml = new StringBuilder(256);
		xml.append("<xml>");

		cdata(xml, "ToUserName", response.getToUserName());
		cdata(xml, "FromUserName", response.getFromUserName());
		cdata(xml, "CreateTime", response.getCreateTime().getTime() / 1000);
		cdata(xml, "MsgType", response.getType().getText());

		if (response instanceof TextResponse)
		{
			buildText(xml, (TextResponse)response);
		}
		else if (response instanceof ImageResponse)
		{
			buildImage(xml, (ImageResponse)response);
		}
		else if (response instanceof NewsResponse)
		{
			buildNews(xml, (NewsResponse)response);
		}
		else if (response instanceof MusicResponse)
		{
			buildMusic(xml, (MusicResponse)response);
		}
		else if (response instanceof VoiceResponse)
		{
			buildVoice(xml, (VoiceResponse)response);
		}
		else if (response instanceof VideoResponse)
		{
			buildVideo(xml, (VideoResponse)response);
		}

		xml.append("</xml>");
		return xml.toString();
	}

	private static void buildText(StringBuilder xml, TextResponse response)
	{
		cdata(xml, "Content", response.getContent());
	}

	private static void buildNews(StringBuilder xml, NewsResponse response)
	{
		if (response.getItemList() == null)
			return;

		cdata(xml, "ArticleCount", response.getItemList().size());
		xml.append("<Articles>");
		for (ArticleItem item : response.getItemList())
		{
			xml.append("<item>");
			cdata(xml, "Title", item.getTitle());
			cdata(xml, "Description", item.getDescription());
			cdata(xml, "PicUrl", item.getPicUrl());
			cdata(xml, "Url", item.getUrl());
			xml.append("</item>");
		}
		xml.append("</Articles>");
	}
	
	private static void buildMusic(StringBuilder xml, MusicResponse response)
	{
		xml.append("<Music>");
		cdata(xml, "Title", response.getTitle());
		cdata(xml, "Description", response.getDescription());
		cdata(xml, "MusicUrl", response.getMusicUrl());
		cdata(xml, "HQMusicUrl", response.getHqMusicUrl());
		cdata(xml, "ThumbMediaId", response.getThumbMediaId());
		xml.append("</Music>");
	}
	
	private static void buildVideo(StringBuilder xml, VideoResponse response)
	{
		xml.append("<Video>");
		cdata(xml, "MediaId", response.getMediaId());
		cdata(xml, "Title", response.getTitle());
		cdata(xml, "Description", response.getDescription());
		xml.append("</Video>");
	}
	
	private static void buildVoice(StringBuilder xml, VoiceResponse response)
	{
		xml.append("<Voice>");
		cdata(xml, "MediaId", response.getMediaId());
		xml.append("</Voice>");
	}

	private static void buildImage(StringBuilder xml, ImageResponse response)
	{
		xml.append("<Image>");
		cdata(xml, "MediaId", response.getMediaId());
		xml.append("</Image>");
	}

	private static void cdata(StringBuilder xml, String tag, String value)
	{
		xml.append("<").append(tag).append(">");
		xml.append("<![CDATA[").append(value == null ? "" : value).append("]]>");
		xml.append("</").append(tag).append(">");
		xml.append("\n");
	}

	private static void cdata(StringBuilder xml, String tag, long value)
	{
		xml.append("<").append(tag).append(">");
		xml.append(value);
		xml.append("</").append(tag).append(">");
		xml.append("\n");
	}
}
